function transpose(level)
{
	var newLevel = [];
	for(var row = 0;row < level.length;row++)
	{
		for(var column = 0;column < level[row].length;column++)
		{
			if(newLevel[column] == undefined)
				newLevel[column] = [];
			newLevel[column][row] = level[row][column];
		}
	}
	return newLevel;
}

function reverse(level)
{
	var newLevel = [];
	for(var row = 0;row < level.length;row++)
	{
		newLevel[row] = [];
		for(var column = 0;column < level[row].length;column++)
		{
			newLevel[row][column] = level[row][level[row].length - column - 1];
		}
	}
	return newLevel;
}

function turn(level)
{
	var newLevel = [];
	game.orientation = (game.orientation + 1) % 2;
	if(game.orientation == 1)
	{
		newLevel = reverse(transpose(level));
	}
	else
	{
		newLevel = transpose(reverse(level));
	}
	return newLevel;
}

function getNewPos(r,c,code)
{
	var level = game.CurrentLevel;
	switch(code)
	{
		case 37 :
			c = c - 1;
			break;
		case 38 :
			r = r - 1;
			break;
		case 39 :
			c = c + 1;
			break;
		case 40 :
			r = r + 1;
			break;
	}
	return [r,c];
}

/*function makeModalDialog(h,w,text)
  {
  function modalHandler(e)
  {
  if(!e) var e = window.event;
  e.stopPropagation();
  }

  var div = document.createElement('div');
  div.style.zIndex = "100";
  div.style.position = "fixed";
  div.style.left = "0px";
  div.style*/

function inputEvent(code)
{
	var row = game.playerPos[0],column = game.playerPos[1];
	var newpos;
	switch(code)
	{
		case 8 :
			onBackSpace();
			break;
		case 81 : // Q
			game.currentLevel = turn(game.currentLevel);
			break;
		case 82 : 
			redoMove();
			break;
		case 85 :
			undoMove();
			break;
		case 83 :
			changeLevel(game.episode,game.levelNumber);
			break;

		case 37 : // Left
		case 38 : // Up
		case 39 : // Right
		case 40 : // Down
			newpos = getNewPos(row,column,code);
			/*
				#(35) - Wall
				@(64) - Is where 'we' are on the floor
				+(43) - Is where 'we' are on an empty slot
				.(46) - Is an empty sink
			 	*(42) - is a filled sink
			 	$(36) - is an out of position block
			 	(32) - a blank cell, we put a space in there, is a normal cell
			 */
			/*
			   What can happen when we move to a new spot?
			   if newpos is on a wall, we return
			   if newpos is on a floor, we move
			   if newpos is onto an empty slot, we move
			   if newpos is on a block, move and move block
			   if newpos is on a filled slot, move, move block, and empty filled slot
			 */
			var level = game.currentLevel;
			if(level[newpos[0]][newpos[1]] == 35)
				return;
			else if(level[newpos[0]][newpos[1]] == 32 || level[newpos[0]][newpos[1]] == 46)
			{
				if(level[row][column] == 64)
					level[row][column] = 32;
				else if(level[row][column] == 43)
					level[row][column] = 46;
				else
					abort("we were on a non-empty slot!");
				if(level[newpos[0]][newpos[1]] == 32)
					level[newpos[0]][newpos[1]] = 64;
				else if(level[newpos[0]][newpos[1]] == 46)
					level[newpos[0]][newpos[1]] = 43;
				game.playerPos[0] = newpos[0];
				game.playerPos[1] = newpos[1];
			}
			else if(level[newpos[0]][newpos[1]] == 42 || level[newpos[0]][newpos[1]] == 36)
			{
				var blockpos = getNewPos(newpos[0],newpos[1],code);
				if(level[blockpos[0]][blockpos[1]] == 35 ||level[blockpos[0]][blockpos[1]] == 42 || level[blockpos[0]][blockpos[1]] == 36)
					return;
				if(level[blockpos[0]][blockpos[1]] == 46)
					level[blockpos[0]][blockpos[1]] = 42;
				else if(level[blockpos[0]][blockpos[1]] == 32)
					level[blockpos[0]][blockpos[1]] = 36;
				if(level[newpos[0]][newpos[1]] == 42)
					level[newpos[0]][newpos[1]] = 43;
				else if(level[newpos[0]][newpos[1]] == 36)
					level[newpos[0]][newpos[1]] = 64;
				if(level[row][column] == 64)
					level[row][column] = 32;
				else if(level[row][column] == 43)
					level[row][column] = 46;
				game.playerPos[0] = newpos[0];
				game.playerPos[1] = newpos[1];
			}
			addMove();
			break;

		default :
			console.log('got ' + code);
				return;
	}
	layoutLevel();
	if(endOfLevel())
		showEndOfLevelMenu();
}

function undoMove()
{
	if(moveListIndex > 0)
	{
		moveListIndex--;
		game.currentLevel = copyLevel(moveList[moveListIndex]);
	}
}

function redoMove()
{
	if(moveListIndex < moveList.length - 1)
	{
		moveListIndex++;
		game.currentLevel = copyLevel(moveList[moveListIndex]);
	}
}

function addMove()
{
	if(moveListIndex != moveList.length - 1)
	{
		moveList = moveList.splice(0,moveListIndex + 1);
	}
	moveListIndex++;
	moveList[moveListIndex] = copyLevel(game.currentLevel);
}

function endOfLevel()
{
	var level = game.currentLevel;
	for(var row = 0;row < level.length;row++)
		for(var column = 0;column < level[row].length;column++)
		{
			if(level[row][column] == 46 || level[row][column] == 43)
				return false;
		}

	return true;
}

function showEndOfLevelMenu()
{
	var r = confirm("Level complete! Press OK to go the next level, Cancel to go back...");
	if(r == true)
	{
		game.levelNumber += 1;
		changeLevel(game.episode,game.levelNumber);
		layoutLevel();
	}
	else
	{
		console.log('quitting');
	}
}

function handleInput(e)
{
	if(!e) var e = window.event;
	var code = (e.keyCode) ? e.keyCode : e.which;

	inputEvent(code);
}
